package com.ems.service.impl.Invigilate.room;

import com.ems.dao.Invigilate.room.RoomPaperDao;
import com.ems.dao.Invigilate.room.RoomUserDao;
import com.ems.pojo.Invigilate.room.RoomPaper;
import com.ems.pojo.Invigilate.room.RoomUser;
import com.ems.pojo.Invigilate.room.TestRoom;
import com.ems.pojo.user.User;
import com.ems.service.Invigilate.room.RoomUserService;
import com.ems.service.Invigilate.room.TestRoomService;
import com.ems.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 伟伟
 * @Date: 2020/2/18
 * @Description: 描述
 * @Version: 1.0
 */
@Service("roomUserService")
public class RoomUserServiceImpl implements RoomUserService {
    @Autowired
    private RoomUserDao roomUserDao;

    @Autowired
    private RoomPaperDao roomPaperDao;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("testRoomService")
    private TestRoomService testRoomService;

    //获取考场中的人数
    @Override
    public List<RoomUser> getRoomUserByTrId(int tr_id) {
        return roomUserDao.getRoomUserByTrId(tr_id);
    }

    //获取所有申请考场的信息

    @Override
    public List<RoomUser> getUnRoomUserByTrId(int tr_id) {
        List<RoomUser> roomUserList = roomUserDao.getUnRoomUserByTrId(tr_id);
        //获取用户
        if(roomUserList.size() > 0){
            RoomUser roomUser = null;
            for(int i = 0;i < roomUserList.size();i ++){
                roomUser = roomUserList.get(i);
                roomUser.setUser(userService.getUserById(roomUser.getId()));
            }
        }
        return roomUserList;
    }

    //同意考生加入指定的考场
    @Override
    public void allowUser(RoomUser roomUser) {
         roomUserDao.allowUser(roomUser);
    }

    //删除考生的申请信息

    @Override
    public void deleteAllowUser( List<RoomUser> roomUsers) {
        if(roomUsers != null && roomUsers.size() > 0){
            for(int i = 0;i < roomUsers.size();i ++){
                roomUserDao.deleteAllowUser(roomUsers.get(i));
            }
        }
    }

    //获取监考教师下面所有的考生信息
    @Override
    public List<User> getRoomUserInfo(Map<String, Object> map, User user) {
        //获取所有的考场id
        List<TestRoom> testRoomList = testRoomService.selectAllTestRoomById(user);
        if(testRoomList.size() > 0){
            //根据考场的id获取考场中所有考生的id
            List<RoomUser> roomUserList = roomUserDao.getAllRoomUserByTrId(testRoomList);
            if(roomUserList.size() > 0){
                //根据考生id查询考生信息
                return userService.getUserInfoByListId(roomUserList,map);
            }else{
                return new ArrayList<User>();
            }

        }else{
            return new ArrayList<User>();
        }
    }

    @Override
    public void deleteAlreadyUser(List<User> userList) {
        if(userList != null && userList.size() > 0){
            for(int i = 0;i < userList.size();i ++){
                User user = userList.get(i);
                //删除考生的考试信息

                //从考场中删除考生信息
                roomUserDao.deleteRoomUserById(user.getId());
            }
        }

    }

    //查询学生要申请进入的考场信息

    @Override
    public List<RoomUser> getStudentApply(Map<String, Object> map, User user) {
        List<RoomUser> list = roomUserDao.getStudentApply(user.getId(),(String) map.get("tr_name"));
        if(list.size() > 0){
            TestRoom testRoom = null;
            for(int i = 0;i < list.size();i ++){
                testRoom = list.get(i).getTestRoom();
                //根据id查询用户信息
                User user1 = userService.getUserById(testRoom.getId());
                testRoom.setUser(user1);
            }
        }
        return list;
    }

    //考试删除要申请进入的考场信息
    @Override
    public void deleteApplyTestRoom(List<RoomUser> list,User user) {
       if(list != null && list.size() > 0){
           for(int i = 0;i < list.size();i ++){
               roomUserDao.deleteApplyTestRoom(list.get(i).getRu_id(),user.getId());
           }
       }
    }

    //考生申请进入指定的考场

    @Override
    public int agreeApplyTestRoom(TestRoom testRoom, User user) {
        //根据名称和考场代码查询对应的考场
        TestRoom testRoom1 = testRoomService.getTestRoomByPwd(testRoom);
        if(testRoom1 != null && testRoom1.getTr_id() != 0){
            //提交用户的申请信息
            RoomUser roomUser = new RoomUser();
            roomUser.setTr_id(testRoom1.getTr_id());
            roomUser.setId(user.getId());
            roomUserDao.addAgreeRoomUser(roomUser);
            return 1;
        }else{
            return -1;
        }
    }

    //查询考生已进入的考场信息
    @Override
    public List<RoomUser> getAlreadyTestRooInfo(Map<String, Object> map, User user) {
        //根据id查询同意的考场信息
        map.put("id",user.getId());
        List<RoomUser> roomUserList = roomUserDao.getAlreadyTestRooInfo(map);
        if(roomUserList.size() > 0){
            TestRoom testRoom = null;
            for(int i = 0;i < roomUserList.size();i ++){
                //查询监考信息
                testRoom = roomUserList.get(i).getTestRoom();
                User user1 = userService.getUserById(testRoom.getId());
                testRoom.setUser(user1);

                //获取考场中的人数
                List<RoomUser> roomUserList1 = roomUserDao.getRoomUserByTrId(testRoom.getTr_id());
                testRoom.setUserNum(roomUserList1.size());
                //获取考场中的考题数
                List<RoomPaper> roomPaperList =roomPaperDao.getRoomPaperByTrId(testRoom.getTr_id());
                testRoom.setRoomPaperNum(roomPaperList.size());
            }
        }
        return roomUserList;
    }

    //考生退出考场
    @Override
    public void exitAlreadyTestRoom(List<RoomUser> roomUserList) {
        if(roomUserList != null){
            for(int i = 0;i < roomUserList.size();i ++){
                //删除该考生在退出考场的考试信息

                //退出考场
                roomUserDao.exitAlreadyTestRoom(roomUserList.get(i));
            }
        }
    }

    @Override
    public void deleteRoomUserById(List<User> list) {
        if(list != null && list.size() > 0){
            User user = null;
            for(int i = 0;i < list.size();i ++){
                user = list.get(i);
                roomUserDao.deleteRoomUserById(user.getId());
            }
        }
    }

    @Override
    public void deleteRoomUserByTrId(int tr_id) {
        roomUserDao.deleteRoomUserByTrId(tr_id);
    }
}
